﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using CsGL.OpenGL;
using System.IO;
using GMPP.Properties;

namespace GMPP.Texture
{
    public class Texture
    {
        public uint[] texture = null;	// Storage For Our one Texture

        // public uint texInx;

        public Texture(List<String> paths, string path, uint filterType, out Dictionary<String, uint> sceneTexs)
        {
            var colors = new List<string>();
            var texPaths = new List<string>();
            foreach (var t in paths)
            {
                if (t.Length>=5&&t.Substring(0, 5).ToUpper() == "COLOR")
                {
                    colors.Add(t);
                }
                else
                {
                    texPaths.Add(t);
                }
            }

            var image = new Bitmap[texPaths.Count];
            texture = new uint[texPaths.Count];
            sceneTexs = new Dictionary<string, uint>();


            try
            {

                for (int i = 0; i < texPaths.Count; i++)
                    if (File.Exists(Path.Combine(path, texPaths[i] )))
                    {
                        image[i] = new Bitmap(Path.Combine(path, texPaths[i]));
                    }
                    else
                    {
                        image[i] = new Bitmap(Path.Combine(path, "unkownTex.jpg"));
                    }

            }
            catch (System.ArgumentException)
            {
                MessageBox.Show(Resources.fileNotFound, Resources.Error, MessageBoxButtons.OK);

            }

            GL.glGenTextures(image.Length, texture);
            for (int i = 0; i < image.Length; i++)
            {
                if (image[i] != null)
                {
                    image[i].RotateFlip(RotateFlipType.RotateNoneFlipY);
                    var rect = new Rectangle(0, 0, image[i].Width, image[i].Height);

                    var bitmapdata = image[i].LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                    // Create Linear Filtered Texture
                    OpenGL.glBindTexture(OpenGL.GL_TEXTURE_2D, texture[i]);
                    GL.glTexParameteri(GL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
                    GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
                    GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB, image[i].Width, image[i].Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE, bitmapdata.Scan0);

                    image[i].UnlockBits(bitmapdata);
                    image[i].Dispose();

                    sceneTexs.Add(paths[i], texture[i]);
                }
            }
            foreach (var t in colors)
            {
                sceneTexs.Add(colors[0], 0);
            }
        }



    }
}
